﻿/*
 * Created by SharpDevelop.
 * User: CeCi
 * Date: 11/04/2014
 * Time: 07:27 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;
using Gestión_UTN.Entities;
using System.Collections.Generic;

namespace Gestión_UTN.daimpl
{
	/// <summary>
	/// Description of mailProfesordaimpl.
	/// </summary>
	public class mailProfesordaimpl:da.mailProfesorda
	{
		public mailProfesordaimpl()
		{
		}
		
		public bool SaveMailProfesor(int pro_dni, string correo)
		{
			bool ingresado = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "INSERT INTO cop_MailProfesores(pro_dni, cop_correo)" +
                    			"VALUES(@dni, @correo)";
					
					OleDbCommand insertar = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					
					object[] values = {pro_dni,correo};
					
					insertar.Parameters.AddRange(values);
					
					insertar.ExecuteNonQuery();
					
					trans.Commit();
					
					ingresado = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				ingresado = false;
				trans.Rollback();
				conex.Close();
			}
			
			return ingresado;
		}
		
		public bool DeleteMailProfesor(int pro_dni, string correo)
		{
			bool eliminado = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "DELETE FROM cop_MailProfesores " +
						"WHERE pro_dni = @dni AND cop_correo = @correo";
					
					OleDbCommand eliminar = new OleDbCommand(sql,conex);
					
					conex.Open();
					
					trans = conex.BeginTransaction();
					
					eliminar.Parameters.AddWithValue("@dni",pro_dni);
					eliminar.Parameters.AddWithValue("@correo",correo);
					
					eliminar.ExecuteNonQuery();
					
					trans.Commit();
					
					eliminado = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				eliminado = false;
				trans.Rollback();
				conex.Close();
			}
			
			return eliminado;
		}
		
		public bool ModifyMailProfesor(int pro_dni_anterior, string correo_anterior, int pro_dni, string correo)
		{
			bool modificado = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "UPDATE cop_MailProfesores SET pro_dni = @dni," +
									" cop_correo = @correo " +
									"WHERE pro_dni = @dni_anterior AND cop_correo = @correo_anterior";
					
					OleDbCommand modificar = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					
					object[] values = {pro_dni,correo,pro_dni_anterior,correo_anterior};
					
					modificar.Parameters.AddRange(values);
					
					modificar.ExecuteNonQuery();
					
					trans.Commit();
					
					modificado = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				modificado = false;
				trans.Rollback();
				conex.Close();
			}
			
			return modificado;
		}
		
		public System.Data.DataTable FindAllMailProfesores()
		{
			DataTable tablaMailPro = null;
			
			OleDbConnection conex = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				tablaMailPro = new DataTable();
				
				if (null != conex) {
					
					string sql = "SELECT pro_dni, pro_nombre, pro_apellido, cop_correo" +
									" FROM cop_MailProfesores cop, pro_profesores pro " +
									"WHERE cop.pro_dni = pro.pro_dni";
					
					 OleDbDataAdapter adaptador = new OleDbDataAdapter(sql, conex);
					 
					 conex.Open();
					 
					 adaptador.Fill(tablaMailPro,"mailProfesores");
					
					 conex.Close();
				}
				
			} catch (Exception) {
				
				tablaMailPro = null;
				conex.Close();
			}
			
			return tablaMailPro;
		}
		
		public System.Collections.Generic.List<Gestión_UTN.Entities.MailProfesor> FindMailsByDni(int pro_dni)
		{
			List<MailProfesor> listaMails = null;
			
			OleDbConnection conex = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				listaMails = new List<MailProfesor>();
				
				if (null != conex) {
					
					string sql = "SELECT pro_dni, cop_correo" +
									" FROM cop_MailProfesores cop " +
									"WHERE pro_dni = @dni";
					
					OleDbCommand consulta = new OleDbCommand(sql,conex);
					
					conex.Open();
					
					consulta.Parameters.AddWithValue("@dni",pro_dni);
					
					OleDbDataReader lector = consulta.ExecuteReader();
					
					while (lector.Read()) 
					{
						MailProfesor mail = new MailProfesor(Convert.ToInt16(lector[0].ToString()),
						                                        lector[1].ToString());
						listaMails.Add(mail);
						
						mail = null;
					}
					
					conex.Close();
				}
				
			} catch (Exception) {
				
				listaMails = null;
				conex.Close();
			}
			
			return listaMails;
		}
	}
}
